//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package objects;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import common.GlobalID;
import common.Utils;
import init.StaticData;
import location.DisplayObjectTimer;
import location.DisplayProductionObject;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

public abstract class ProductionFactories {
    private LinkedHashMap<String, Integer> componentNameToId = new LinkedHashMap();
    private LinkedHashMap<String, Integer> goodsPlanning = new LinkedHashMap();
    protected LinkedHashMap<Integer, DisplayProductionObject> productions = new LinkedHashMap();
    private ProductionQueue queue = new ProductionQueue();
    private boolean enableLog = false;

    public ProductionFactories() {
    }

    public abstract String getProductionName();

    public abstract int getObjectClass();

    public abstract boolean checkUnlockLevel(ProductionsGoodEntry var1, Integer var2);

    public abstract int getUnitsMultiplier();

    public JsonObject getAsJsonObject(String name, Integer id) {
        JsonObject jf = new JsonObject();
        jf.addProperty("Name", name);
        jf.addProperty("ID", id);
        JsonArray jInst = new JsonArray();
        Iterator var6 = this.productions.values().iterator();

        while(var6.hasNext()) {
            DisplayProductionObject dpo = (DisplayProductionObject)var6.next();
            jInst.add(dpo.getAsJsonObject());
        }

        jf.add("ProductionInstances", jInst);
        return jf;
    }

    public JsonObject getAsJsonObject() {
        FactoryEntry f = ProcessingBuildings.getByName(this.getProductionName());
        if(f != null) {
            return this.getAsJsonObject(f.getName(), Integer.valueOf(f.getId()));
        } else {
            AnimalEntry a = Animals.getByName(this.getProductionName());
            if(a != null) {
                return this.getAsJsonObject(a.getName(), a.getGlobalID());
            } else {
                FruitTreeEntry ft = FruitTrees.getByName(this.getProductionName());
                return ft != null?this.getAsJsonObject(ft.getName(), ft.getGlobalID()):new JsonObject();
            }
        }
    }

    public void add(DisplayProductionObject obj) {
        this.productions.put(Integer.valueOf(obj.getProductionInstanceGlobalID()), obj);
    }

    public DisplayProductionObject get(int idx) {
        return (DisplayProductionObject)this.productions.get(Integer.valueOf(idx));
    }

    public void clear() {
        this.productions.clear();
    }

    public int getEmptyCount() {
        int ec = 0;

        DisplayProductionObject dpo;
        for(Iterator var3 = this.productions.values().iterator(); var3.hasNext(); ec += dpo.getEmptySlotsCount()) {
            dpo = (DisplayProductionObject)var3.next();
        }

        return ec;
    }

    public void speedup(int ticksNow) {
        Iterator var3 = this.productions.values().iterator();

        while(var3.hasNext()) {
            DisplayProductionObject dpo = (DisplayProductionObject)var3.next();
            dpo.speedup(ticksNow);
        }

    }

    public int getEmptyCount(Integer pgi) {
        int ec = 0;
        Iterator var4 = this.productions.values().iterator();

        while(var4.hasNext()) {
            DisplayProductionObject dpo = (DisplayProductionObject)var4.next();
            if(dpo.getProductionInstanceGlobalID() == pgi.intValue()) {
                ec = dpo.getEmptySlotsCount();
                break;
            }
        }

        return ec;
    }

    public Integer getMaxEmptyIdentifier() {
        int ec = 0;
        Integer rid = null;
        Iterator var4 = this.productions.values().iterator();

        while(var4.hasNext()) {
            DisplayProductionObject dpo = (DisplayProductionObject)var4.next();
            int cur = dpo.getEmptySlotsCount();
            if(ec < cur) {
                ec = cur;
                rid = Integer.valueOf(dpo.getProductionInstanceGlobalID());
            }
        }

        return rid;
    }

    public int getReadyCount() {
        int rc = 0;

        DisplayProductionObject dpo;
        for(Iterator var3 = this.productions.values().iterator(); var3.hasNext(); rc += dpo.getReadySlotsCount()) {
            dpo = (DisplayProductionObject)var3.next();
        }

        return rc;
    }

    public int getInProgressCount() {
        int val = 0;

        DisplayProductionObject dpo;
        for(Iterator var3 = this.productions.values().iterator(); var3.hasNext(); val += dpo.getInprogressCount()) {
            dpo = (DisplayProductionObject)var3.next();
        }

        return val;
    }

    public int getSlotsCount() {
        int val = 0;

        DisplayProductionObject dpo;
        for(Iterator var3 = this.productions.values().iterator(); var3.hasNext(); val += dpo.rank()) {
            dpo = (DisplayProductionObject)var3.next();
        }

        if(val == 0) {
            val = 1;
        }

        return val;
    }

    public int size() {
        return this.productions.size();
    }

    public void updateTicksLeft(int ticksNow) {
        Iterator var3 = this.productions.values().iterator();

        while(var3.hasNext()) {
            DisplayProductionObject dpo = (DisplayProductionObject)var3.next();
            dpo.updateTicksLeft(ticksNow);
        }

    }

    public ProductionsGoodEntry getGoodById(Integer id) {
        FactoryEntry fe = ProcessingBuildings.getByName(this.getProductionName());
        return fe == null?null:fe.getGoodById(id);
    }

    public ProductionsGoodEntry getGoodByName(String name) {
        FactoryEntry fe = ProcessingBuildings.getByName(this.getProductionName());
        return fe == null?null:fe.getGoodByName(name);
    }

    public Collection<ProductionsGoodEntry> getGoods() {
        FactoryEntry fe = ProcessingBuildings.getByName(this.getProductionName());
        return (Collection)(fe == null?new ArrayList():fe.getGoods());
    }

    public Integer produce(Integer dpoGlobalId, Integer id, int ticksNow) {
        DisplayProductionObject actDpo = null;
        Iterator timeSeconds = this.productions.values().iterator();

        while(timeSeconds.hasNext()) {
            DisplayProductionObject sge = (DisplayProductionObject)timeSeconds.next();
            if(sge.getProductionInstanceGlobalID() == dpoGlobalId.intValue()) {
                actDpo = sge;
            }
        }

        ProductionsGoodEntry sge1 = this.getGoodById(id);
        if(sge1 != null && actDpo != null) {
            int timeSeconds1 = sge1.getPruduceTime().intValue();
            int ticksLeft = Utils.secondsToObjectTicks(timeSeconds1);
            DisplayObjectTimer tmr = new DisplayObjectTimer(timeSeconds1, ticksLeft, ticksNow);
            actDpo.addProductionTimer(id.intValue(), tmr);
            return dpoGlobalId;
        } else {
            return null;
        }
    }

    public Integer produce(int id, int ticksNow) {
        DisplayProductionObject actionSmelter = null;
        Iterator timeSeconds = this.productions.values().iterator();

        while(timeSeconds.hasNext()) {
            DisplayProductionObject sge = (DisplayProductionObject)timeSeconds.next();
            if(sge.getEmptySlotsCount() > 0) {
                actionSmelter = sge;
                break;
            }
        }

        if(actionSmelter == null) {
            return null;
        } else {
            ProductionsGoodEntry sge1 = this.getGoodById(Integer.valueOf(id));
            if(sge1 == null) {
                return null;
            } else {
                int timeSeconds1 = sge1.getPruduceTime().intValue();
                int ticksLeft = Utils.secondsToObjectTicks(timeSeconds1);
                DisplayObjectTimer tmr = new DisplayObjectTimer(timeSeconds1, ticksLeft, ticksNow);
                actionSmelter.addProductionTimer(id, tmr);
                return Integer.valueOf(actionSmelter.getProductionInstanceGlobalID());
            }
        }
    }

    public LinkedHashMap<Integer, List<Integer>> claimReady(int maxClimeCount) {
        LinkedHashMap ready = new LinkedHashMap();
        int climedCount = 0;
        Iterator var5 = this.productions.values().iterator();

        while(var5.hasNext()) {
            DisplayProductionObject dpo = (DisplayProductionObject)var5.next();
            if(dpo.getReadySlotsCount() > 0) {
                List l = dpo.climeReady(maxClimeCount - climedCount);
                climedCount += l.size();
                if(l.size() > 0) {
                    ready.put(Integer.valueOf(dpo.getProductionInstanceGlobalID()), l);
                }
            }

            if(climedCount == maxClimeCount) {
                break;
            }
        }

        return ready;
    }

    public Integer getGoodsCount(Integer id) {
        Integer val = Integer.valueOf(0);

        DisplayProductionObject dpo;
        for(Iterator var4 = this.productions.values().iterator(); var4.hasNext(); val = Integer.valueOf(val.intValue() + dpo.getGoodsCount(id).intValue())) {
            dpo = (DisplayProductionObject)var4.next();
        }

        return val;
    }

    public LinkedHashMap<String, Integer> neededToProduce(String goods, LinkedHashMap<Integer, Integer> inventory, int level, Integer hcLimit) {
        ProductionsGoodEntry pge = this.getGoodByName(goods);
        if(pge != null && this.checkComponentsUnlockLevel(pge, Integer.valueOf(level))) {
            LinkedHashMap notpresent = new LinkedHashMap();
            boolean yes = this.isRequirementsPresent(pge.getRequirements(), inventory, notpresent, hcLimit);
            if(!yes) {
                return notpresent;
            }
        }

        return null;
    }

    public boolean isRequirementsPresent(LinkedHashMap<String, Integer> req, LinkedHashMap<Integer, Integer> inventory, LinkedHashMap<String, Integer> notpresent, Integer hcLimit) {
        boolean present = true;
        Iterator var7 = req.keySet().iterator();

        while(var7.hasNext()) {
            String goodMaterial = (String)var7.next();
            Integer reqId = this.componentIdByName(goodMaterial);
            Integer reqAmount = (Integer)req.get(goodMaterial);
            if(reqId == null || reqAmount == null) {
                return false;
            }

            Integer curAmount = (Integer)inventory.get(reqId);
            if(curAmount == null) {
                curAmount = Integer.valueOf(0);
            }

            if(GlobalID.getClassID(reqId.intValue()) == 4) {
                reqAmount = Integer.valueOf(reqAmount.intValue() + hcLimit.intValue());
            }

            if(curAmount.intValue() < reqAmount.intValue()) {
                present = false;
                if(notpresent == null) {
                    return false;
                }

                if(!ResourceRequest.isLocked(goodMaterial, this.getProductionName())) {
                    ResourceRequest.lock(goodMaterial, this.getProductionName());
                }

                notpresent.put(goodMaterial, Integer.valueOf(reqAmount.intValue() - curAmount.intValue()));
            } else {
                ResourceRequest.unlock(goodMaterial);
            }
        }

        return present;
    }

    public Integer componentIdByName(String name) {
        return (Integer)this.componentNameToId.get(name);
    }

    public void addReqirementComponentPair(String name, Integer id) {
        this.componentNameToId.put(name, id);
    }

    private boolean checkRequirementComponents(LinkedHashMap<String, Integer> rq) {
        Iterator var3 = rq.keySet().iterator();

        while(var3.hasNext()) {
            String componentName = (String)var3.next();
            if(this.componentIdByName(componentName) == null) {
                return false;
            }
        }

        return true;
    }

    public LinkedHashMap<String, Integer> getOverallComponentsNeeded(Integer unlockLevel) {
        LinkedHashMap val = new LinkedHashMap();
        Iterator var4 = this.getGoods().iterator();

        while(true) {
            LinkedHashMap clist;
            do {
                ProductionsGoodEntry pge;
                do {
                    if(!var4.hasNext()) {
                        return val;
                    }

                    pge = (ProductionsGoodEntry)var4.next();
                } while(unlockLevel != null && !this.checkComponentsUnlockLevel(pge, unlockLevel));

                clist = pge.getRequirements();
            } while(!this.checkRequirementComponents(clist));

            String componentName;
            Integer amount;
            for(Iterator var7 = clist.keySet().iterator(); var7.hasNext(); val.put(componentName, Integer.valueOf(amount.intValue() + ((Integer)clist.get(componentName)).intValue()))) {
                componentName = (String)var7.next();
                amount = (Integer)val.get(componentName);
                if(amount == null) {
                    amount = Integer.valueOf(0);
                }
            }
        }
    }

    public boolean checkComponentsUnlockLevel(ProductionsGoodEntry pge, Integer unlockLevel) {
        return pge.getUnlockLevel() == null?this.checkUnlockLevel(pge, unlockLevel):unlockLevel.intValue() >= pge.getUnlockLevel().intValue();
    }

    public void generateDefaultPlanningList() {
        this.goodsPlanning.clear();
        Iterator var2 = this.getGoods().iterator();

        while(var2.hasNext()) {
            ProductionsGoodEntry pge = (ProductionsGoodEntry)var2.next();
            if(this.checkRequirementComponents(pge.getRequirements())) {
                this.goodsPlanning.put(pge.getName(), Integer.valueOf(1));
            }
        }

    }

    public void setGoodsPlanningList(LinkedHashMap<String, Integer> pl) {
        LinkedHashMap oldList = this.goodsPlanning;
        this.goodsPlanning = pl;
        boolean isNewList = false;
        Object[] a = oldList.keySet().toArray();
        Object[] b = pl.keySet().toArray();
        if(a.length != b.length) {
            isNewList = true;
        } else {
            for(int i = 0; i < a.length; ++i) {
                String sa = (String)a[i];
                String sb = (String)b[i];
                if(!sa.equals(sb)) {
                    isNewList = true;
                    break;
                }

                Integer ia = (Integer)oldList.get(sa);
                Integer ib = (Integer)pl.get(sb);
                if(ia.intValue() != ib.intValue()) {
                    isNewList = true;
                    break;
                }
            }
        }

        if(isNewList) {
            oldList.clear();
            this.queue.init(pl);
        }

    }

    public String headQueue() {
        return this.queue != null?this.queue.head():null;
    }

    public String shiftQueue() {
        return this.queue.shiftHead();
    }

    public String queueToString() {
        return this.queue.queueToString();
    }

    public LinkedHashMap<String, Integer> getGoodsPlanningList(Integer unlockLevel) {
        if(unlockLevel == null) {
            return this.goodsPlanning;
        } else {
            LinkedHashMap val = new LinkedHashMap();
            Iterator var4 = this.goodsPlanning.keySet().iterator();

            while(var4.hasNext()) {
                String key = (String)var4.next();
                ProductionsGoodEntry pge = this.getGoodByName(key);
                if(pge != null && this.checkComponentsUnlockLevel(pge, unlockLevel)) {
                    val.put(key, (Integer)this.goodsPlanning.get(key));
                }
            }

            return val;
        }
    }

    public void createRequirementComponentPairs() {
        Iterator var2 = this.getGoods().iterator();

        label26:
        while(var2.hasNext()) {
            ProductionsGoodEntry pge = (ProductionsGoodEntry)var2.next();
            Iterator var4 = pge.getRequirements().keySet().iterator();

            while(true) {
                while(true) {
                    if(!var4.hasNext()) {
                        continue label26;
                    }

                    String reqName = (String)var4.next();
                    Integer reqId = StaticData.getGlobalId(reqName);
                    if(reqId != null && GlobalID.getClassID(reqId.intValue()) != 0) {
                        this.addReqirementComponentPair(reqName, reqId);
                    } else {
                        System.err.println(pge.getProcessingBuilding() + ": addReqirementComponentPair(" + reqName + ", null) \n");
                    }
                }
            }
        }

    }

    public boolean logEnable() {
        return this.enableLog;
    }

    public void logEnable(boolean val) {
        this.enableLog = val;
    }

    public boolean virtualProduction() {
        return false;
    }

    public boolean onlyOneDpoPerSlot() {
        return false;
    }

    public class claimedFruits {
        public final Integer fruitId;
        public final Integer fruitIndex;

        public claimedFruits(Integer fruitId, Integer fruitIndex) {
            this.fruitId = fruitId;
            this.fruitIndex = fruitIndex;
        }
    }
}
